package Jets.ui.skins
{
   import flash.display.BitmapData;
   import flash.events.Event;
   import flash.geom.Rectangle;
   
   import Jets.display.JSprite;
   
   public class Skin
   {
	   
	   public static var skins:Object = {};
	   
	   protected var normal:*;
	   protected var over:*;
	   protected var down:*;
	   protected var select:*;
	   protected var rect:Rectangle;
	   protected var repeat:Boolean;
	   
	   public var name:String;
	   
	   /**
	   * 默认的皮肤构造类，此类不用new方法创建，通过静态函数addSkin创建
	   * @param nomal 
	   * @param over
	   * @param down
	   * @param select
	   * @param rect
	   * @param repeat 是否一直显示nomal背景 用于叠加select状态 如checkbox
	   * 
	   */
      public function Skin(nomal:*, over:*=null, down:*=null, select:*=null, rect:*=null,repeat:Boolean=false) 
	  {
         this.normal = nomal;
         this.over = over;
         this.down = down;
         this.select = select;
         this.rect = rect;
		 this.repeat = repeat;
      }
      
	  public static function addColorSkin(skinName:String,nomal:Object,over:Object=null,down:Object=null,select:Object=null,repeat:Boolean=false):void
	  {
		  skins[skinName] = new Skin(nomal,over,down,select,null,repeat);
		  skins[skinName].name = skinName;
	  }
	  
	  public static function addBmpSkin(skinName:String,nomal:BitmapData,over:BitmapData=null,down:BitmapData=null,select:BitmapData=null,repeat:Boolean=false):void
	  {
		  skins[skinName] = new BmpSkin(nomal,over,down,select,repeat);
		  skins[skinName].name = skinName;
	  }
	  
	  public static function addBmpSkinCls(skinName:String,nomal:Class,over:Class=null,down:Class=null,select:Class=null,repeat:Boolean=false):void
	  {
		  var bnomal:BitmapData = nomal==null?null:new nomal().bitmapData;
		  var bover:BitmapData = over==null?null:new over().bitmapData;
		  var bdown:BitmapData = down==null?null:new down().bitmapData;
		  var bselect:BitmapData = select==null?null:new select().bitmapData;
		  skins[skinName] = new BmpSkin(bnomal,bover,bdown,bselect,repeat);
		  skins[skinName].name = skinName;
	  }
	  
	  public static function addBmp9Skin(skinName:String,nomal:BitmapData,over:BitmapData=null,down:BitmapData=null,select:BitmapData=null,rect:Rectangle=null,repeat:Boolean=false):void
	  {
		  skins[skinName] = new Bmp9Skin(nomal,over,down,select,rect,repeat);
		  skins[skinName].name = skinName;
	  }
     
	  /**
	  *添加一种皮肤
	  * @param skinName 皮肤的名称
	  * @param skinType 皮肤的类型 有SkinType指定类型
	  * @param nomal 默认皮肤 皮肤的类型可以是BitmapData 或者是 color颜色
	  * @param over
	  * @param down
	  * @param select
	  * @param rect 9png各种缩放数据保存
	  * @param repeat 是否重复利用
	  */
	  public static function addSkin(skinName:String,//皮肤的名称
										 skinType:uint,//皮肤的类型 有SKinType指定类型
										 nomal:*,//普通状态下的显示
										 over:*=null,//鼠标移动上去
										 down:*=null,//鼠标按下
										 select:*=null,//选中
										 rect:Rectangle=null,//为.9.png指定的缩放数据
										 repeat:Boolean=false//是否是重复利用的资源
		  								):void
	  {
		  
		  switch(skinType)
		  {
			  case SkinType.Image:
				  skins[skinName] = new BmpSkin(nomal,over,down,select,repeat);
				  break;
			  case SkinType.Color:
				  skins[skinName] = new Skin(nomal,over,down,select,rect,repeat);
				  break;
			  case SkinType.NineBmp:
				  skins[skinName] = new Bmp9Skin(nomal,over,down,select,rect,repeat);
				  break;
			  default:
				  throw new Error("Add Skin Type Error!");
				  return;
		  }
		  skins[skinName].name = skinName;
		  
	  }
	  
      
      public static function hasSkin(skinName:String) : Boolean 
	  {
         return !(skins[skinName] == null);
      }
      
      public static function getSkin(skinName:String) : Skin 
	  {
         return skins[skinName];
      }
      

      public function hook(dis:JSprite) : void 
	  {
         dis.addEventListener(JSprite.DRAW,this.draw);
      }
      
      public function unhook(dis:JSprite) : void 
	  {
         dis.removeEventListener(JSprite.DRAW,this.draw);
      }
      
      public function draw(event:Event) : void 
	  {
         var color:uint = 0;
         var ui:JSprite = event.target as JSprite;
         switch(ui.state)
         {
            case SkinStates.OVER:
				color = this.over;
               break;
            case SkinStates.SELECTED:
				color = this.select;
               break;
            case SkinStates.DOWN:
				color = this.down;
               break;
            default:
				color = this.normal;
         }
         
		 color = (color) || (this.normal);
		 ui.graphics.clear();
		 ui.graphics.beginFill(color,1);
		 ui.graphics.drawRect(0,0,ui.width,ui.height);
		 ui.graphics.endFill();
      }
   }
}
